package main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.Scanner;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
//import java.nio.file.CopyOption;
//import java.nio.file.Files;
//import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author Yan Couto
 */
public class FileManager {

//    private static ArrayList<File> files = new ArrayList<File>();

    protected FileManager () {}

    public static Scanner getReader (File f) {
        try {
            return new Scanner(new BufferedReader(new FileReader(f)));
        } catch (Exception ex) {
            return null;
        }
    }
    
//    public static ObjectInputStream getObjectReader (File f) {
//        try {
//            return new ObjectInputStream(new FileInputStream(f));
//        } catch (Exception ex) {
//            return null;
//        }
//    }

    public static PrintWriter getWriter (File f, boolean append) {
        try {
            return new PrintWriter(new BufferedWriter(new FileWriter(f, append)), true);
        } catch (Exception ex) {
            return null;
        }
    }
//    
//    public static ObjectOutputStream getObjectWriter (File f,boolean append) {
//        try {
//            return new ObjectOutputStream(new FileOutputStream(f,append));
//        } catch (Exception ex) {
//            return null;
//        }
//    }

    public static String[] getFullText(File f) {
        ArrayList<String> lines = new ArrayList<String>();
        Scanner sc = getReader(f);
        while(sc.hasNextLine()) {
            lines.add(sc.nextLine());
        }
        String[] a = new String[lines.size()];
        return lines.toArray(a);
    }

//    public static File[] getFiles (File dir) {
//        if(dir.isFile()) return null;
//        getFilesAux(dir);
//        File[] f = new File[files.size()];
//        f = files.toArray(f);
//        files.clear();
//        return f;
//    }

//    private static void getFilesAux (File f) {
//        File[] sons = f.listFiles();
//        for(File son : sons) {
//            if(son.isDirectory())
//                getFilesAux(son);
//            else
//                files.add(son);
//        }
//    }
    
//    public static boolean isEmpty (File f) {
//        if(f.isDirectory() && f.listFiles() == null)
//            return true;
//        if(f.isFile() && !getReader(f).hasNext())
//            return true;
//        return false;
//    }
//    
//    public static void copy(File origFile,File destFile,CopyOption... co) throws IOException {
//        File bak = null;
//        boolean backup = false;
//        if(destFile.exists()) {
//            Random r = new Random();
//            bak = File.createTempFile(destFile.getName() + r.nextInt(), null);
//            copyNoBak(destFile,bak,StandardCopyOption.REPLACE_EXISTING);
//            backup = true;
//        }
//       
//        try {
//            copyNoBak(origFile,destFile,co);
//        } catch(IOException e) {
//            if(backup) {
//            boolean worked = true;
//            System.err.println("Error occurred, trying to fix it...");
//            try {
//            copyNoBak(bak,destFile,StandardCopyOption.REPLACE_EXISTING);
//            } catch(IOException ee) {
//                System.err.println("... Didn't work. Why it didn't:");
//                ee.printStackTrace();
//                System.err.println("\n");
//                worked = false;
//            }
//            if(worked) System.err.println("...Apparently worked.");
//            System.err.println("This is the original error:");
//            }
//            throw e;
//        } finally {
//            if(backup) {
//                delete(bak);
//            }
//        }
//    }
    
//    private static void copyNoBak(File origFile,File destFile,CopyOption... co) throws IOException {
//        if(!(destFile.exists() && destFile.isDirectory())) 
//            Files.copy(origFile.toPath(), destFile.toPath(), co);
//        if(origFile.isFile()) return; 
//        File[] sons = origFile.listFiles();
//        for(File f:sons) 
//            copy(f,new File(destFile.getPath() + File.separator + f.getName()),co);
//    }
    
//    public static Document getDocument(File f) {
//        try{
//            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//            factory.setIgnoringComments(true);
//            factory.setIgnoringElementContentWhitespace(true);
//            DocumentBuilder builder = factory.newDocumentBuilder();
//            return builder.parse(f.toURI().toString());
//        }catch(ParserConfigurationException | SAXException | IOException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
//    
//    public static Document newDocument() {
//        try{
//            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//            factory.setIgnoringComments(true);
//            factory.setIgnoringElementContentWhitespace(true);
//            factory.setValidating(true);
//            DocumentBuilder builder = factory.newDocumentBuilder();
//            return builder.newDocument();
//        }catch(ParserConfigurationException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
    
//    public static String getXMLString(Document doc) {
//        try {
//            Transformer t = TransformerFactory.newInstance().newTransformer();
//            t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
//            t.setOutputProperty(OutputKeys.INDENT, "yes");
//            t.setOutputProperty(OutputKeys.METHOD, "xml");
//            
//            StringWriter sw = new StringWriter();
//            StreamResult res = new StreamResult(sw);
//            DOMSource docxml = new DOMSource(doc);
//            t.transform(docxml, res);
//            return sw.toString();
//        } catch (TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException ex) {
//            ex.printStackTrace();
//            return null;
//        }
//    }

//    public static void delete(File f) {
//        if(f.isFile()||FileManager.isEmpty(f))
//            f.delete();
//        else {
//            File[] sons = f.listFiles();
//
//            for(File son : sons)
//                delete(son);
//            f.delete();
//        }
//    }

//    public static void  deleteAllIn (File f) {
//            File[] sons = f.listFiles();
//            for(File son : sons)
//                delete(son);
//    }
    public static void addFilter(JFileChooser toAdd,String extension,String description) {
        String[] ext = {extension};
        String[] desc = {description};
        addFilters(toAdd,ext,desc);
    }
    
    public static void addFilters(JFileChooser toAdd,String[] extension,String[] description) {
        String regex[][] = new String[extension.length][];
        for(int i=0;i<regex.length;i++)
            regex[i] = extension[i].split("\t");
        for(int i=0;i<regex.length;i++) {
            for(int k=0;k<regex[i].length;k++) {
                regex[i][k] = (regex[i][k].startsWith(".")? "" : ".") + regex[i][k];
            }
        }
        if(regex.length != description.length)
            throw new IllegalArgumentException();
        int count = regex.length;
        for(int i=0;i<count;i++) {
            final String[] reg = regex[i];
            final String desc = description[i];
            toAdd.addChoosableFileFilter(new FileFilter () {

                @Override
                public boolean accept(File f) {
                    if(f.isDirectory())
                        return true;
                    boolean b = false;
                    for(String r : reg)
                        if(f.getName().endsWith(r)) 
                            b=true;
                    return b;
                }

                @Override
                public String getDescription() {
                    String d = desc + " (";
                    for(String r: reg) {
                        d += r;
                        if(r != reg[reg.length-1])
                            d+=",";
                    }
                    d+= ")";
                    return d;
                }
                
            });
        }
    }
}
